import java.util.Arrays;

public class MergeSort {
    public static void mergeSort(int[] array) {
        mergeSplit(array, 0, array.length - 1);
    }

    private static void mergeSplit(int[] array, int left, int right) {
        if (left >= right) return;
        int mid = left + (right - left) / 2;
        mergeSplit(array, left, mid);
        mergeSplit(array, mid + 1, right);
        merge(array, left, right, mid);
    }
    private static void merge(int[] array, int left, int right, int mid) {
        int s1 = left;
        int s2 = mid + 1;
        int i = 0;
        int[] tmp = new int[right - left + 1];
        while (s1 <= mid && s2 <= right) {
            if (array[s1] <= array[s2]) {
                tmp[i++] = array[s1++];
            } else {
                tmp[i++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[i++] = array[s1++];
        }
        while (s2 <= right) {
            tmp[i++] = array[s2++];
        }
        for (int j = 0; j < tmp.length; j++) {
            array[j + left] = tmp[j];
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{7,3,5,4,8,10};
        mergeSort(array);
        System.out.println(Arrays.toString(array));
    }
}
